/*
	The MIT License

	Copyright (c) 2007 Mike Chambers

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#import "URLService.h"

//timeout value for the http request
#define TIMEOUT 10.0

@implementation URLService

//takes a url and returns a string of the contents.
//if an error occurs, it will return nil, and populate the specified error
-(NSString *) retrieveURL: (NSURL *) url error: (NSError **) error
{
	//create the http request for the resource to retrieve
	NSURLRequest *theRequest=[NSURLRequest requestWithURL:url
								cachePolicy:NSURLRequestUseProtocolCachePolicy
								timeoutInterval:TIMEOUT];
					
	//holds the response headers
	NSURLResponse *response = nil;
	
	//this will capture any errors
	NSError *e;
	
	//make the connection to the server and get the data
	NSData *data = [ NSURLConnection sendSynchronousRequest: theRequest
				returningResponse: &response
				error: &e ];
	
	//check and see if any errors occured
	if(e != nil)
	{
		//populate the passed in error
		*error = [NSError errorWithDomain:@"turlError" code:1 userInfo:nil];
		return nil;
	}
	
	//get a string from the response and return it
	return [[NSString alloc] initWithData:data encoding:NSMacOSRomanStringEncoding];
}

//simple API that encodes reserved characters according to:
//RFC 3986
//http://tools.ietf.org/html/rfc3986
+(NSString *) urlencode: (NSString *) url
{
	NSArray *escapeChars = [NSArray arrayWithObjects:@";" , @"/" , @"?" , @":" ,
														@"@" , @"&" , @"=" , @"+" ,
														@"$" , @"," , @"[" , @"]",
														@"#", @"!", @"'", @"(", 
														@")", @"*", nil];
														 
	NSArray *replaceChars = [NSArray arrayWithObjects:@"%3B" , @"%2F" , @"%3F" ,
														@"%3A" , @"%40" , @"%26" ,
														@"%3D" , @"%2B" , @"%24" ,
														@"%2C" , @"%5B" , @"%5D", 
														@"%23", @"%21", @"%27",
														@"%28", @"%29", @"%2A", nil];
	
	int len = [escapeChars count];

	NSMutableString *temp = [url mutableCopy];
	
	int i;
	for(i = 0; i < len; i++)
	{
	
		[temp replaceOccurrencesOfString: [escapeChars objectAtIndex:i]
									withString:[replaceChars objectAtIndex:i]
									options:NSLiteralSearch
									range:NSMakeRange(0, [temp length])];
	}
	
	NSString *out = [NSString stringWithString: temp];
	
	return out;
}

@end
